package com.uobgroup.ews.uam.core.dao;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.uobgroup.ews.uam.core.domain.AccessControl;
import com.uobgroup.ews.uam.core.domain.CombinationACL;
import com.uobgroup.ews.uam.core.domain.CombinationMatrix;
import com.uobgroup.ews.uam.core.domain.SecurityGroup;

@Repository
public class CombinationACLDaoImpl extends AbstractJpaDao<CombinationACL> implements CombinationACLDao {

   @SuppressWarnings("unchecked")
   @Override
   public List<AccessControl> findACLs(CombinationMatrix combinationMatrix) {
      String queryStr = "select combiACL.accessControl from CombinationACL combiACL where combiACL.combination = :combinationMatrix";
      Query query = entityManager.createQuery(queryStr, AccessControl.class);
      query.setParameter("combinationMatrix", combinationMatrix);
      return query.getResultList();
   }

   @Override
   public SecurityGroup findSecGroup(CombinationMatrix combinationMatrix, AccessControl acl) {
      String queryStr = "select combiACL.securityGroup from CombinationACL combiACL where combiACL.combination = :combinationMatrix "
            + "and combiACL.accessControl = :acl";
      Query query = entityManager.createQuery(queryStr, SecurityGroup.class);
      query.setParameter("combinationMatrix", combinationMatrix);
      query.setParameter("acl", acl);
      return (SecurityGroup)query.getSingleResult();
   }

   @SuppressWarnings("unchecked")
   @Override
   public List<CombinationMatrix> findCombinations(AccessControl acl) {
      String queryStr = "select combiACL.combination from CombinationACL combiACL where combiACL.accessControl = :acl";
      Query query = entityManager.createQuery(queryStr, CombinationMatrix.class);
      query.setParameter("acl", acl);
      return query.getResultList();
   }
   
}
